package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.model.databaseobject.ModuleDo;
import com.centanet.framework.glide.GlideLoad;

import java.util.ArrayList;

/**
 * 首页菜单
 * Created by guilin on 15/12/23.
 */
public class HomeMenuAdapter extends BaseAdapter {

    public interface MenuItemClick {
        void itemClick(ModuleDo moduleDo);
    }

    private MenuItemClick menuItemClick = new MenuItemClick() {
        @Override
        public void itemClick(ModuleDo moduleDo) {
            if (itemClick != null) {
                itemClick.itemClick(moduleDo);
            }
        }
    };
    private LayoutInflater layoutInflater;
    private DrawableRequestBuilder<String> requestBuilder;
    private ArrayList<ArrayList<ModuleDo>> arrayList;
    private MenuItemClick itemClick;

    public HomeMenuAdapter(Context context, DrawableRequestBuilder<String> requestBuilder,
                           ArrayList<ArrayList<ModuleDo>> arrayList, MenuItemClick itemClick) {
        layoutInflater = LayoutInflater.from(context);
        this.requestBuilder = requestBuilder;
        this.arrayList = arrayList;
        this.itemClick = itemClick;
    }

    @Override
    public int getCount() {
        int size = 0;
        for (ArrayList<ModuleDo> moduleDos : arrayList) {
            if (moduleDos == null || moduleDos.size() < 3) {
                continue;
            }
            size++;
        }
        return size;
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return arrayList.get(position).size() == 3 ? 0 : 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int itemViewType = getItemViewType(position);
        MenuVH3 menuVH3 = null;
        MenuVH4 menuVH4 = null;
        if (convertView == null) {
            switch (itemViewType) {
                case 0:
                    menuVH3 = new MenuVH3();
                    convertView = inflate(parent, R.layout.item_home_menu_three);

                    menuVH3.ll_home_menu_1 = (LinearLayout) convertView.findViewById(R.id.ll_home_menu_1);
                    menuVH3.atv_home_menu_1 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_1);
                    menuVH3.img_home_menu_1 = (ImageView) convertView.findViewById(R.id.img_home_menu_1);
                    menuVH3.rl_home_menu_2 = (RelativeLayout) convertView.findViewById(R.id.rl_home_menu_2);
                    menuVH3.atv_home_menu_2 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_2);
                    menuVH3.atv_home_menu_3 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_3);
                    menuVH3.img_home_menu_2 = (ImageView) convertView.findViewById(R.id.img_home_menu_2);
                    menuVH3.rl_home_menu_3 = (RelativeLayout) convertView.findViewById(R.id.rl_home_menu_3);
                    menuVH3.atv_home_menu_4 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_4);
                    menuVH3.atv_home_menu_5 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_5);
                    menuVH3.img_home_menu_3 = (ImageView) convertView.findViewById(R.id.img_home_menu_3);

                    convertView.setTag(menuVH3);
                    break;
                case 1:
                    menuVH4 = new MenuVH4();
                    convertView = inflate(parent, R.layout.item_home_menu_four);

                    menuVH4.rl_home_menu_1 = (RelativeLayout) convertView.findViewById(R.id.rl_home_menu_1);
                    menuVH4.rl_home_menu_2 = (RelativeLayout) convertView.findViewById(R.id.rl_home_menu_2);
                    menuVH4.rl_home_menu_3 = (RelativeLayout) convertView.findViewById(R.id.rl_home_menu_3);
                    menuVH4.rl_home_menu_4 = (RelativeLayout) convertView.findViewById(R.id.rl_home_menu_4);

                    menuVH4.atv_home_menu_1 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_1);
                    menuVH4.atv_home_menu_2 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_2);
                    menuVH4.atv_home_menu_3 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_3);
                    menuVH4.atv_home_menu_4 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_4);
                    menuVH4.atv_home_menu_5 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_5);
                    menuVH4.atv_home_menu_6 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_6);
                    menuVH4.atv_home_menu_7 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_7);
                    menuVH4.atv_home_menu_8 = (AppCompatTextView) convertView.findViewById(R.id.atv_home_menu_8);

                    menuVH4.img_home_menu_1 = (ImageView) convertView.findViewById(R.id.img_home_menu_1);
                    menuVH4.img_home_menu_2 = (ImageView) convertView.findViewById(R.id.img_home_menu_2);
                    menuVH4.img_home_menu_3 = (ImageView) convertView.findViewById(R.id.img_home_menu_3);
                    menuVH4.img_home_menu_4 = (ImageView) convertView.findViewById(R.id.img_home_menu_4);
                    convertView.setTag(menuVH4);
                default:
                    break;
            }
        } else {
            switch (itemViewType) {
                case 0:
                    menuVH3 = (MenuVH3) convertView.getTag();
                    break;
                case 1:
                    menuVH4 = (MenuVH4) convertView.getTag();
                    break;
                default:
                    break;
            }
        }
        switch (itemViewType) {
            case 0:
                final ArrayList<ModuleDo> menu1 = arrayList.get(position);

                menuVH3.atv_home_menu_1.setText(menu1.get(0).getName());
                menuVH3.atv_home_menu_2.setText(menu1.get(1).getName());
                menuVH3.atv_home_menu_3.setText(menu1.get(1).getDesc());
                menuVH3.atv_home_menu_4.setText(menu1.get(2).getName());
                menuVH3.atv_home_menu_5.setText(menu1.get(2).getDesc());

                GlideLoad.load(new GlideLoad.Builder(requestBuilder, menu1.get(0).getIcon())
                        .fitCenter()
                        .into(menuVH3.img_home_menu_1));
                GlideLoad.load(new GlideLoad.Builder(requestBuilder, menu1.get(1).getIcon())
                        .fitCenter()
                        .into(menuVH3.img_home_menu_2));
                GlideLoad.load(new GlideLoad.Builder(requestBuilder, menu1.get(2).getIcon())
                        .fitCenter()
                        .into(menuVH3.img_home_menu_3));

                menuVH3.ll_home_menu_1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        menuItemClick.itemClick(menu1.get(0));
                    }
                });
                menuVH3.rl_home_menu_2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        menuItemClick.itemClick(menu1.get(1));
                    }
                });
                menuVH3.rl_home_menu_3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        menuItemClick.itemClick(menu1.get(2));
                    }
                });
                break;
            case 1:
                final ArrayList<ModuleDo> menu2 = arrayList.get(position);

                menuVH4.atv_home_menu_1.setText(menu2.get(0).getName());
                menuVH4.atv_home_menu_2.setText(menu2.get(0).getDesc());
                menuVH4.atv_home_menu_3.setText(menu2.get(1).getName());
                menuVH4.atv_home_menu_4.setText(menu2.get(1).getDesc());
                menuVH4.atv_home_menu_5.setText(menu2.get(2).getName());
                menuVH4.atv_home_menu_6.setText(menu2.get(2).getDesc());
                menuVH4.atv_home_menu_7.setText(menu2.get(3).getName());
                menuVH4.atv_home_menu_8.setText(menu2.get(3).getDesc());

                GlideLoad.load(new GlideLoad.Builder(requestBuilder, menu2.get(0).getIcon())
                        .fitCenter()
                        .into(menuVH4.img_home_menu_1));
                GlideLoad.load(new GlideLoad.Builder(requestBuilder, menu2.get(1).getIcon())
                        .fitCenter()
                        .into(menuVH4.img_home_menu_2));
                GlideLoad.load(new GlideLoad.Builder(requestBuilder, menu2.get(2).getIcon())
                        .fitCenter()
                        .into(menuVH4.img_home_menu_3));
                GlideLoad.load(new GlideLoad.Builder(requestBuilder, menu2.get(3).getIcon())
                        .fitCenter()
                        .into(menuVH4.img_home_menu_4));

                menuVH4.rl_home_menu_1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        menuItemClick.itemClick(menu2.get(0));
                    }
                });
                menuVH4.rl_home_menu_2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        menuItemClick.itemClick(menu2.get(1));
                    }
                });
                menuVH4.rl_home_menu_3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        menuItemClick.itemClick(menu2.get(2));
                    }
                });
                menuVH4.rl_home_menu_4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        menuItemClick.itemClick(menu2.get(3));
                    }
                });
                break;
            default:
                break;
        }

        return convertView;
    }

    private View inflate(ViewGroup parent, @LayoutRes int id) {
        return layoutInflater.inflate(id, parent, false);
    }

    class MenuVH3 {
        LinearLayout ll_home_menu_1;
        AppCompatTextView atv_home_menu_1, atv_home_menu_2, atv_home_menu_3, atv_home_menu_4, atv_home_menu_5;
        ImageView img_home_menu_1, img_home_menu_2, img_home_menu_3;
        RelativeLayout rl_home_menu_2, rl_home_menu_3;
    }

    class MenuVH4 {
        AppCompatTextView atv_home_menu_1, atv_home_menu_2, atv_home_menu_3, atv_home_menu_4,
                atv_home_menu_5, atv_home_menu_6, atv_home_menu_7, atv_home_menu_8;
        ImageView img_home_menu_1, img_home_menu_2, img_home_menu_3, img_home_menu_4;
        RelativeLayout rl_home_menu_1, rl_home_menu_2, rl_home_menu_3, rl_home_menu_4;
    }
}
